#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 410, M = N * N;
int h1[N], h2[N], e[M], ne[M], idx;
int dist[N], g[N][N];
bool st[N];
int n, m;
void add(int* h, int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int spfa(int* h)
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    st[1] = true;
    queue<int> q;
    q.push(1);
    while (q.size())
    {
        int t = q.front();
        q.pop();
        st[t] = false;
        for (int i = h[t]; ~i; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[t] + 1)
            {
                dist[j] = dist[t] + 1;
                if (st[j] == false)
                {
                    q.push(j);
                    st[j] = true;
                }
            }
        }
    }
    if (dist[n] > 0x3f3f3f3f / 2) return -1;
    else return dist[n];
}
int main()
{
    cin >> n >> m;
    memset(h1, -1, sizeof h1);
    memset(h2, -1, sizeof h2);
    for (int i = 1; i <= m; i++)
    {
        int a, b;
        cin >> a >> b;
        g[a][b] = g[b][a] = 1;
        add(h1, a, b), add(h1, b, a);
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            if (g[i][j] == 0)
            {
                add(h2, i, j), add(h2, j, i);
            }
        }
    }
    int ans = 0;
    if (g[1][n] == 0) ans = spfa(h1);
    else ans = spfa(h2);
    cout << ans << endl;
    return 0;
}